package com.asifrc.tw101;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.junit.Assert.assertEquals;

/**
 * Created by asifrc on 2/11/14.
 */
public class ExerciseTest {
    private Exercise exercise = new Exercise();
    private ByteArrayOutputStream output = new ByteArrayOutputStream();

    @Before
    public void setUp() throws Exception {
        System.setOut(new PrintStream(output));
    }

    @After
    public void tearDown() throws Exception {
        System.setOut(null);
    }

    @Test
    public void testPrintOne() throws Exception {
        exercise.printOne();
        assertEquals("*\n\n", output.toString());
    }

    @Test
    public void testPrintHorizontalLine() throws Exception {
        String three = "***\n\n";
        String eight = "********\n\n";
        exercise.printHorizontalLine(3);
        assertEquals(three, output.toString());
        exercise.printHorizontalLine(8);
        assertEquals(three+eight, output.toString());
    }

    @Test
    public void testPrintVerticalLine() throws Exception {
        String three = "*\n*\n*\n\n";
        String eight = "*\n*\n*\n*\n*\n*\n*\n*\n\n";
        exercise.printVerticalLine(3);
        assertEquals(three, output.toString());
        assertEquals(three+eight, output.toString());
    }

    @Test
    public void testDrawRight() throws Exception {
        String three = "*\n**\n***\n\n";
        String eight = "*\n**\n***\n****\n*****\n******\n*******\n********\n\n";
        exercise.drawRight(3);
        assertEquals(three, output.toString());
        exercise.drawRight(8);
        assertEquals(three+eight, output.toString());
    }

    @Test
    public void testDrawIsosceles() throws Exception {
        String three = "  *\n ***\n*****\n\n";
        String eight = "       *\n      ***\n     *****\n    *******\n";
        eight += "   *********\n  ***********\n *************\n***************\n\n";
        exercise.drawIsosceles(3);
        assertEquals(three, output.toString());
        exercise.drawIsosceles(8);
        assertEquals(three+eight, output.toString());
    }

    @Test
    public void testDrawDiamond() throws Exception {
        String three = "  *\n ***\n*****\n ***\n  *\n\n";
        String eight = "       *\n      ***\n     *****\n    *******\n";
        eight += "   *********\n  ***********\n *************\n***************\n";
        eight += " *************\n  ***********\n   *********\n";
        eight += "    *******\n     *****\n      ***\n       *\n\n";
        exercise.drawDiamond(3);
        assertEquals(three, output.toString());
        exercise.drawDiamond(8);
        assertEquals(three+eight, output.toString());
    }

    @Test
    public void testDrawDiamondWithName() throws Exception {
        String name = "Asif";
        String three = "  *\n ***\n" + name + "\n ***\n  *\n\n";
        String eight = "       *\n      ***\n     *****\n    *******\n";
        eight += "   *********\n  ***********\n *************\n" + name + "\n";
        eight += " *************\n  ***********\n   *********\n";
        eight += "    *******\n     *****\n      ***\n       *\n\n";
        exercise.drawDiamondWithName(3, name);
        assertEquals(three, output.toString());
        exercise.drawDiamondWithName(8, name);
        assertEquals(three+eight, output.toString());
    }
}
